//
//  WJ_Essence_VC.m
//  WJBSApp
//
//  Created by dllo on 16/7/18.
//  Copyright © 2016年 王景伟. All rights reserved.
//
// 看了别人的代码 有如下想法  先建立底部视图控制器(自定义)  给予这个控制器 在上面创建好多子控制器(也可以自定义)

// 所以第一步建立WJEssAndNewBelowVC
// 第二部建立上面的 WJEssAndNewUpTableVC

// 所以这个也要以 WJEssAndNewBelowVC 为基类 上面建立 WJEssAndNewUpTableVC

#import "WJ_Essence_VC.h"
#import "WJEssAndNewUpTableVC.h"
#import "WJ_Through_VC.h"
#import "WJDataManager.h"
#import "WJMeModel.h"


@interface WJ_Essence_VC ()

@property (nonatomic,strong) NSMutableArray *array;

@end

@implementation WJ_Essence_VC

//每次进来都要执行
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
    
    if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"LogInEssEnce"] isEqualToString:@"2"]) {
        
#pragma mark - 添加强大的逻辑运算 1. 如果相同 不作任何操作 2.以前的数组删掉了   3.以前的数组添加了 不写了太麻烦
        
    // 移除以前的
        for (WJEssAndNewBelowVC *vc in self.childViewControllers) {
          
            for (NSString *str in self.array) {
                
                if ([vc.title isEqualToString:str]) {
                    [vc removeFromParentViewController];
                    [self.topCollectionView reloadData];
                    [self.belowCollectionView reloadData];
                }
                
            }
        }
        
        //所有的 全部的 移除掉了  接下来添加新的
        for (WJMeModel *model in [[WJDataManager shareFMDB] selectAllFir]) {
            
            NSString *url = [NSString stringWithFormat:@"http://s.budejie.com/topic/tag-topic/%ld/hot/bs0315-iphone-4.3/0-20.json",(long)model.theme_id];
            
            WJEssAndNewUpTableVC *vc = [[WJEssAndNewUpTableVC alloc]init];
            vc.title = model.theme_name;
            vc.url = url;
            [self addChildViewController:vc];
            [vc release];
            [self.topCollectionView reloadData];
            [self.belowCollectionView reloadData];
            [self.array addObject:vc.title];
            
        }

    }else{
        [[NSUserDefaults standardUserDefaults] setValue:@"2" forKey:@"LogInEssEnce"];
        
    }
    
    
    
    
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.array = [NSMutableArray array];
    
    [self createConfig];
    
    [self addChildViewController];
}

- (void)createConfig
{
    self.navigationItem.title = @"精华";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"RandomAcross~iphone.png" hightImage:@"RandomAcrossClick~iphone.png" target:self action:@selector(essenceRightAction)];
}

- (void)addChildViewController
{
    //推荐
    WJEssAndNewUpTableVC *allVc = [[WJEssAndNewUpTableVC alloc]init];
    allVc.title = @"推荐";
    allVc.url = WJEssenceRecommendURL;
    [self addChildViewController:allVc];
    [allVc release];
    
    //视频
    WJEssAndNewUpTableVC *videoVc = [[WJEssAndNewUpTableVC alloc]init];
    videoVc.title = @"视频";
    videoVc.url = WJEssenceVideoURL;
    [self addChildViewController:videoVc];
    [videoVc release];
    
    //图片
    WJEssAndNewUpTableVC *pictureVc = [[WJEssAndNewUpTableVC alloc]init];
    pictureVc.title = @"图片";
    pictureVc.url = WJEssencePictureURL;
    [self addChildViewController:pictureVc];
    [pictureVc release];
    
    //段子
    WJEssAndNewUpTableVC *textVc = [[WJEssAndNewUpTableVC alloc]init];
    textVc.title = @"段子";
    textVc.url = WJEssenceTextURL;
    [self addChildViewController:textVc];
    [textVc release];
    
    //网红
    WJEssAndNewUpTableVC *starVc = [[WJEssAndNewUpTableVC alloc]init];
    starVc.title = @"网红";
    starVc.url = WJEssenceStartURL;
    [self addChildViewController:starVc];
    [starVc release];
    
    //排行
    WJEssAndNewUpTableVC *rankingVc = [[WJEssAndNewUpTableVC alloc]init];
    rankingVc.title = @"排行";
    rankingVc.url = WJEssenceListsURL;
    [self addChildViewController:rankingVc];
    [rankingVc release];
    
    //社会
    WJEssAndNewUpTableVC *societyVc = [[WJEssAndNewUpTableVC alloc]init];
    societyVc.title = @"社会";
    societyVc.url = WJEssenceSocietyURL;
    [self addChildViewController:societyVc];
    [societyVc release];
    
    //美女
    WJEssAndNewUpTableVC *girlVc = [[WJEssAndNewUpTableVC alloc]init];
    girlVc.title = @"美女";
    girlVc.url = WJEssenceGirlURL;
    [self addChildViewController:girlVc];
    [girlVc release];
    
    //冷知识
    WJEssAndNewUpTableVC *cold = [[WJEssAndNewUpTableVC alloc]init];
    cold.title = @"冷知识";
    cold.url = WJEssenceColdURL;
    [self addChildViewController:cold];
    [cold release];
    
    //游戏
    WJEssAndNewUpTableVC *gameVc = [[WJEssAndNewUpTableVC alloc]init];
    gameVc.title = @"游戏";
    gameVc.url = WJEssenceGameURL;
    [self addChildViewController:gameVc];
    [gameVc release];
    
    // 降低一个数组里面的加入到第一个界面
    for (WJMeModel *model in [[WJDataManager shareFMDB] selectAllFir]) {
        
        NSString *url = [NSString stringWithFormat:@"http://s.budejie.com/topic/tag-topic/%ld/hot/bs0315-iphone-4.3/0-20.json",(long)model.theme_id];
        
        WJEssAndNewUpTableVC *vc = [[WJEssAndNewUpTableVC alloc]init];
        vc.title = model.theme_name;
        vc.url = url;
        [self addChildViewController:vc];
        [vc release];
        [self.topCollectionView reloadData];
        [self.belowCollectionView reloadData];
        [self.array addObject:vc.title];
        
    }
    
}



- (void)essenceRightAction{
    WJ_Through_VC *vc = [[WJ_Through_VC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
